package com.demomaster.quickcompat.widget.radiobutton;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.CompoundButton;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.demomaster.quickcompat.widget.base.QuickLayout;
import com.demomaster.quickcompat.widget.layout.QuickLinearLayout;


public class QuickRadioGroup extends QuickLinearLayout implements QuickLayout {

    private static final String TAG = "QuickRadioGroup";
    private int mCheckedId = NO_ID;
    public QuickRadioGroup(@NonNull Context context) {
        super(context);
        init(context, null, 0);
    }

    public QuickRadioGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public QuickRadioGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(ViewGroup group, @IdRes int checkedId);
    }

    OnCheckedChangeListener onCheckedChangeListener;

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }
    QuickRadioButtonContainer.OnCheckedChangeListener mOnCheckedChangeListener = new QuickRadioButtonContainer.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(QuickRadioButtonContainer buttonView, boolean isChecked) {
            Log.i(TAG,"onCheckedChanged1 checkedId=" + buttonView.getId());
            if(isChecked) {
                checkView(QuickRadioGroup.this, buttonView.getId());
                if (onCheckedChangeListener != null) {
                    onCheckedChangeListener.onCheckedChanged(QuickRadioGroup.this, buttonView.getId());
                }
            }
        }
    };
    CompoundButton.OnCheckedChangeListener mOnCheckedChangeListenerForRadioButton = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.i(TAG, "onCheckedChanged2 checkedId=" + buttonView.getId() + " isChecked=" + isChecked);
            if (isChecked) {
                checkView(QuickRadioGroup.this, buttonView.getId());
                if (onCheckedChangeListener != null) {
                    onCheckedChangeListener.onCheckedChanged(QuickRadioGroup.this, buttonView.getId());
                }
            }
        }
    };

    public int getCheckedRadioButtonId() {
        return mCheckedId;
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.i(TAG, "onFinishInflate c=" + getChildCount());
        if (getChildCount() > 0) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
                setChildListener(child);
            }
        }
    }

    @SuppressLint("ResourceType")
    @Override
    public void onViewAdded(View child) {
        super.onViewAdded(child);
        setChildListener(child);
    }

    private void setChildListener(View view) {
        if (view.getId() == View.NO_ID) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                view.setId(View.generateViewId());
            }
        }
        if (view instanceof Checkable && ((Checkable) view).isChecked()) {
            mCheckedId = view.getId();
        }
        //com.google.android.material.radiobutton.MaterialRadioButton
        if (view instanceof CompoundButton) {
            ((CompoundButton) view).setOnCheckedChangeListener(mOnCheckedChangeListenerForRadioButton);
        } else if (view instanceof QuickRadioButtonContainer) {
            ((QuickRadioButtonContainer) view).setOnCheckedChangeListener(mOnCheckedChangeListener);
        } else if (view instanceof ViewGroup) {
            int count = ((ViewGroup) view).getChildCount();
            for (int i = 0; i < count; i++) {
                View child = ((ViewGroup) view).getChildAt(i);
                setChildListener(child);
            }
        }
    }

    /**
     * 恢复其他child状态
     *
     * @param viewGroup
     * @param selectId
     */
    private void checkView(ViewGroup viewGroup, int selectId) {
        mCheckedId = selectId;
        int count = viewGroup.getChildCount();
        if (viewGroup.getId() == selectId) {
            if (viewGroup instanceof QuickRadioButtonContainer) {
                ((QuickRadioButtonContainer) viewGroup).setChecked(true);
            }
            return;
        }
        for (int i = 0; i < count; i++) {
            View child = viewGroup.getChildAt(i);
            boolean isChecked = (selectId == child.getId());
            if (!isChecked) {
                if (child instanceof Checkable) {
                    ((Checkable) child).setChecked(false);
                    child.setSelected(false);
                } else {
                    if (child instanceof ViewGroup) {
                        checkView((ViewGroup) child, selectId);
                    }
                }
            }
            //QdLog.i("setChildState id="+((View)child).getId()+",sid="+selectId+",child="+child);
        }
    }

    public void check(int id) {
        checkView(this, id);
        if(onCheckedChangeListener!=null){
            onCheckedChangeListener.onCheckedChanged(this, id);
        }
    }
}
